@cd /d %~dp0

@if "%1" == "" (
	echo "no version, usage: publish.cmd 2024.9.12.1"
	pause
	exit
)

echo "set version ......"
python.exe "./../version.py" "%1"
@if not %errorlevel% == 0 (
	echo "failed to set version"
	pause
	exit
)

call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"

:: cd root dir
cd ../../

::delete build dir
@if exist .\..\build-yingce-Release (
	del /Q /S .\..\build-yingce-Release
	rd /Q /S .\..\build-yingce-Release
)
mkdir .\..\build-yingce-Release

D:\Qt6.8\Tools\CMake_64\bin\cmake.exe --build .\..\build-yingce-Release --target clean
D:\Qt6.8\Tools\CMake_64\bin\cmake.exe -S . -B .\..\build-yingce-Release "-DCMAKE_GENERATOR:STRING=Ninja" "-DCMAKE_BUILD_TYPE:STRING=Release" "-DQT_QMAKE_EXECUTABLE:FILEPATH=D:/Qt6.8/6.8.3/msvc2022_64/bin/qmake.exe" "-DCMAKE_PREFIX_PATH:PATH=D:/Qt6.8/6.8.3/msvc2022_64" "-DCMAKE_CXX_FLAGS_INIT:STRING="
D:\Qt6.8\Tools\CMake_64\bin\cmake.exe --build .\..\build-yingce-Release --target all

@if not %errorlevel% == 0 (
	echo "failed to build"
	exit()
)

@copy .\..\build-yingce-Release\src\app\yingce_zh_CN.qm .\..\build-yingce-Release\resources
@copy .\scripts\win\yingce.nsi .\..\build-yingce-Release\
@copy .\src\app\qt.conf .\..\build-yingce-Release\
@copy .\src\libs\thirdparty\crashrpt\bin\x64\crashrpt_lang.ini .\..\build-yingce-Release\
@copy .\src\libs\thirdparty\crashrpt\bin\x64\CrashRpt1403.dll .\..\build-yingce-Release\
@copy .\src\libs\thirdparty\crashrpt\bin\x64\CrashSender1403.exe .\..\build-yingce-Release\

D:/Qt6.8/6.8.3/msvc2022_64/bin/windeployqt.exe --qtpaths D:/Qt6.8/6.8.3/msvc2022_64/bin/qtpaths.exe --plugindir .\..\build-yingce-Release\plugins --exclude-plugins qmltooling --release --force --no-translations .\..\build-yingce-Release\yingce.exe

@copy D:\ffmpeg-7.1.1-full\bin\ffmpeg.exe .\..\build-yingce-Release\

cd .\..\build-yingce-Release\

::makensisw.exe
@set NSISMake="C:\Program Files (x86)\NSIS\makensisw.exe"
%NSISMake% yingce.nsi
@if not %errorlevel% == 0 (
	echo "failed to package"
)

@cd /d %~dp0